package xyz.archknight.app.loginput.dictionary.gen.ui.component

import java.awt.Dimension
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.awt.Insets
import javax.swing.*

/**
 * 基本进度表示对话框
 *
 * @author midnite
 * create at 2020-06-18 10:36 下午
 */
abstract class ProgressDialog(owner: JFrame, title: String) : JDialog(owner) {
    private val constraints = GridBagConstraints()
    protected val processingItem = JLabel("")
    protected val progressLabel = JLabel("0 / 0")
    protected val progressBar = JProgressBar()

    init {
        this.title = title
        createUI()
    }

    private fun createUI() {
        layout = GridBagLayout()
        size = Dimension(350, 100)

        constraints.insets = Insets(4, 8, 4, 8)
        constraints.gridwidth = GridBagConstraints.CENTER
        constraints.fill = GridBagConstraints.BOTH

        addSubComponent(processingItem)
        val separator = JPanel()
        addSubComponent(separator, 1.0)
        constraints.gridwidth = GridBagConstraints.REMAINDER
        addSubComponent(progressLabel)

        constraints.gridwidth = GridBagConstraints.REMAINDER
        addSubComponent(progressBar, 1.0)

        modalityType = ModalityType.APPLICATION_MODAL
        setLocationRelativeTo(null)
    }

    private fun addSubComponent(component: JComponent, weightX: Double = 0.0, weightY: Double = 0.0) {
        constraints.weightx = weightX
        constraints.weighty = weightY
        add(component, constraints)
    }
}